<template>
  <div class="f-all">
    <div class="f-zi">角色管理</div>
    <div class="f-table">
      <div class="f-one">
        <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="formInline">
          <el-form-item label="角色名:">
            <el-input v-model="formInline.role_name" placeholder="角色名"></el-input>
          </el-form-item>
          <el-form-item label="角色状态:">
            <el-select v-model="formInline.role_state" placeholder="全部">
              <el-option
                v-for="item in finput"
                :key="item.role_state"
                :label="item.label"
                :value="item.role_state"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('formInline')" type="primary" >搜索</el-button>
            <el-button type="primary" @click="AddRole()" plain>添加</el-button>
          </el-form-item>
        </el-form>

        <el-dialog title="添加" :visible.sync="dialogFormVisible" width="680px">
          <el-form :model="this.form" ref="form">
            <el-row>
              <el-col :span="11">
                <el-form-item label="角色名:" :label-width="formLabelWidth">
                  <el-input v-model="form.role_name" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="角色描述:" :label-width="formLabelWidth" prop="role_describe">
                  <el-input v-model="form.role_describe" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <el-form-item label="角色编码:" :label-width="formLabelWidth">
                  <el-input v-model="form.role_coding" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="角色状态" :label-width="formLabelWidth">
                  <el-select v-model="form.role_state" placeholder="开启">
                    <el-option label="开启" value="0"></el-option>
                    <el-option label="禁用" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="NoAdd(form)" type="info" plain>取 消</el-button>
            <el-button type="primary" @click="AddSure('form')" plain>确 定</el-button>
          </div>
        </el-dialog>
      </div>

      <el-table
        ref="singleTable"
        :data="currList"
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        border
      >
        <el-table-column type="index" width="100" align="center" label="序号"></el-table-column>
        <el-table-column prop="role_name" label="角色名" align="center"></el-table-column>
        <el-table-column prop="role_describe" label="角色描述" align="center"></el-table-column>
        <el-table-column prop="role_coding" label="角色编码" align="center"></el-table-column>
        <el-table-column prop="role_state" label="角色状态 " align="center">
          <template slot-scope="scope">
            <span class="el-icon-success" style="color:green" v-if="scope.row.role_state=='0'"></span>
            <span class="el-icon-error" style="color:red" v-else></span>
          </template>
        </el-table-column>
        <el-table-column property="do" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.$index, scope.row)">
              <span class="btn-zi">编辑</span>
            </el-button>
            <el-button
              type="text"
              @click="del(scope.$index, scope.row)"
              v-if="scope.row.role_id>1"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 编辑模态框 -->
      <el-dialog title="修改信息" :visible.sync="dialog" width="680px">
        <el-form :model="this.BJform" ref="updataForm">
          <el-row>
            <el-col :span="11">
              <el-form-item label="角色ID:" :label-width="formLabelWidth">
                <el-input v-model="BJform.role_id" autocomplete="off" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="角色名:" :label-width="formLabelWidth">
                <el-input v-model="BJform.role_name" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="角色描述:" :label-width="formLabelWidth">
                <el-input v-model="BJform.role_describe" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="11">
              <el-form-item label="角色编码:" :label-width="formLabelWidth">
                <el-input v-model="BJform.role_coding" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="11">
            <el-form-item label="角色状态:" :label-width="formLabelWidth">
              <el-select
                v-model="BJform.role_state"
                placeholder="全部"
                :disabled="BJform.role_id=='1'?true:false"
              >
                <el-option
                  v-for="item in finput"
                  :key="item.role_id"
                  :label="item.label"
                  :value="item.role_state"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-button class="shezhibtn" @click="shezhibtn(BJform)" type="primary" plain>设置角色下权限</el-button>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="BJqx(BJform)" type="info" plain>取 消</el-button>
          <el-button type="primary" @click="BJsure('updataForm')" plain>确 定</el-button>
        </div>
      </el-dialog>

     
    </div>
     <div class="f-block">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="downinput.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      // 编辑表格
      BJform: {
        role_id: "",
        role_name: "",
        role_describe: "",
        role_coding: "",
        role_state: "",
      },
      // 添加模态框
      dialog: false,
      dialogFormVisible: false,
      form: {
        role_name: "",
        role_describe: "",
        role_coding: "",
        role_state: "",
      },
      formLabelWidth: "120px",
      finput: [
        {
          role_state: "",
          label: "全部",
        },
        {
          role_state: "0",
          label: "启用",
        },
        {
          role_state: "1",
          label: "禁用",
        },
      ],
      tableData: [],
      currPage: "", //当前页
      currList: [], //当前展示的数据数组
      pageSize: 9,
      newData: [],
      fadd: [],
      // 查询input框
      formInline: {
        role_name: "",
        role_state: "",
      },
      downinput: [],
    };
  },
  methods: {
    // 设置管理员按钮
    shezhibtn(BJform) {
      console.log(BJform);
      // var role_id={}

      http
        .findBuildJurisdictions({ role_id: this.BJform.role_id })
        .then((r) => {
          console.log("权限", r);
          console.log({ role_id: this.BJform.role_id });
          this.$router.push({
            name: "设置权限",
            params: this.BJform,
          });
        });
    },
    // 修改模态框取消
    BJqx(BJform) {
      this.dialog = false;
    },
    // 修改模态框确定
    BJsure(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          // this.isAble = false;
          http.updBuildRole(this.BJform).then((r) => {
            console.log("修改的", r);
          });
          this.dialog = false;
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
    // 编辑角色信息
    edit(index, row) {
      this.dialog = true;
      this.BJform = row;
    },
    // 添加模态框里的确定
    AddSure(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          // this.isAble = false;
          http.addBuildRole(this.form).then((r) => {
            http.bottomInput({}).then((r) => {
              this.downinput = r.data;
              this.setCurrList(1);
              console.log("这是添加后的表格", this.downinput);
            });
          });
          // 点取消后清空

          this.dialogFormVisible = false;
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
    // 添加模态框的删除
    NoAdd(form) {
      this.dialogFormVisible = false;
      // 点取消后清空
      for (let key in form) {
        form[key] = "";
      }
    },
    // 添加按钮
    AddRole() {
      this.dialogFormVisible = true;
    },
    // 删除
    del(index, row) {
      let role_id = {};
      role_id.role_id = row.role_id;
      http.delBuildRole(role_id).then((r) => {
        if (r.code == 2) {
          alert("权限不足，无法删除");
        } else {
          console.log("删除", r);
          http.bottomInput({}).then((r) => {
            this.downinput = r.data;
            this.setCurrList(1);
          });
          alert(r.msg)
        }
      });
    },

    // 表格

    // handleCurrentChange(val) {
    //   this.currentRow = val;
    // },
    // 分页
    setCurrList(index) {
      console.log("当前页", index);
      // console.log(this.currPage.length)
      this.currList = this.downinput.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
    },
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);
      console.log("change", val);
    },
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },

    handleSelectionChange(val) {
      this.newData = val;
      console.log(this.newData);
    },
    // 查询
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          http.bottomInput(this.formInline).then((r) => {
            console.log("查询后", r);
            this.downinput = r.data;
            this.setCurrList(1);
            // this.formInline=''
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  mounted() {
    // 权限 http.goit
    http.goit({}).then((r) => {
      if (r.code == 2) {
        alert("权限不足");
        this.$router.push({
          name:"控制台"
        })
      } else {
        console.log(r);
        this.tableData = r.data;
        this.setCurrList(1);
      }
    });
    http.bottomInput({}).then((r) => {
      console.log(r);
      if (r.code == 2) {
        alert("权限不足");
      } else {
        this.downinput = r.data;
        this.setCurrList(1);
        console.log("这是全部表格", this.downinput);
        console.log("这是表格", this.currList);
      }
    });
  },
};
</script>
<style scoped>
.f-all {
  margin-top: 15px;
  width: 100%;
  height: 645px;
  background-color: white;
  overflow-y: scroll;
  position: relative;
}
.f-zi {
  margin-top: 20px;
  font-size: 17px;
  color: #222;
  font-weight: 700;
  margin-left: 15px;
}
.f-table {
  margin-left: 15px;
  margin-top: 10px;
  width: 97%;
}
.f-one {
  width: 100%;
  /* background-color: wheat; */
  display: flex;
}
.f-block {
  position: absolute;
  right: 1%;
  bottom: 5px;
  /* margin-left: 80%;
  margin-top: 15px; */
}
.el-input >>> .el-input__inner {
  width: 185px;
  height: 30px;
  /* margin-top: -20px; */
  padding-left: 5px;
  /* margin-left: 5px; */
}
.el-select >>> .el-input__inner {
  width: 185px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
.el-button {
  height: 32px;
  padding: 5px 10px;
  /* background-color: rgba(68, 83, 199, 0.7); */
  border: cadetblue;

  /* color: white; */
}
.shezhibtn {
  margin-left: 50px;
}
</style>